<?php
class Login_Controller extends Template_Controller {

	protected $mr;
	public $template = 'client/index';
	
    public function __construct()
    {
        parent::__construct();
		 //Css
        $this->template->css = $this->site['theme_url'].'login/login.css';
        //View
        $this->get_view();
    }
    
    public function index()
    {	
        $this->template->content = new View('login/frm_login_register');
		
		if($this->session->get('error_msg'))
			$this->template->content->error_msg = $this->session->get('error_msg');
		if($this->login_model->get_login(1))
		{
			url::redirect('profile');
		}		
    }
    
	private function _get_record()
    {
    	$form = array
	    (
	       'txt_username' => '',
		   'txt_password' => ''	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_username','required');
			$post->add_rules('txt_password','required');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('login_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
			}
        }
    }
    
	private function _valid_login()
	{
		$login = $this->input->xss_clean($this->input->post('txt_username'));
		$pass = $this->input->post('txt_password');
		
		$result = ORM::factory('customer')->where(array('customers_username'=>$login
        , 'customers_pass'=>$pass, 'customers_status'=>'1'))->find();
		
		if($result->count_all())
			return $result->customers_id;
		else		
			return false;		
	}
	
	public function sm()
    {
		$this->login_model->unset_login(1);
		$this->session->delete('info');
		$record = $this->_get_record();
		$valid = $this->_valid_login();
		if ($valid)	// if login access
		{
			$result = ORM::factory('customer',$valid);
			$sess_customer['username'] = $result->customers_username;
			$sess_customer['id'] = $valid;
			$sess_customer['name'] = $result->customers_name;
			$sess_customer['email'] = $result->customers_email;
			$sess_customer['customers_type'] = $result->customers_type;
			
			$this->login_model->set_login(1,$sess_customer);
			
			$flag = false;
			if(isset($this->mr['login_center']))
				if($this->session->get('cart')){
					$cart = $this->session->get('cart');
		    		$this->set_cart($cart);
					if($this->count_products()>0)
						$flag = true;
				}
			if($flag)
				url::redirect('checkout');
			else
				url::redirect('home');			
		} else {
			$this->session->set_flash('error_msg',Kohana::lang('errormsg_lang.error_user_pass'));
			if(isset($this->mr['login_center']))			
				url::redirect('login');
			else
				url::redirect('home');
			die();
		}
	}
	
	public function sm_center()
	{
		$this->mr['login_center'] = 1;
		$this->sm();
	}
	
	public function out()
	{
		$this->login_model->unset_login(1);
		$this->session->delete('info');
		url::redirect('home');		
		die();
	}
}
?>